package com.peace.hospital.opdept.controller.outpatientdept;


import com.alibaba.fastjson.JSON;
import com.peace.hospital.entity.common.*;
import com.peace.hospital.entity.outpatientdept.Chargingitems;
import com.peace.hospital.entity.outpatientdept.Particulars;
import com.peace.hospital.entity.outpatientdept.Patientinformation;
import com.peace.hospital.entity.outpatientdept.dto.RegisteredEnquiryDto;
import com.peace.hospital.entity.outpatientdept.dto.RegistrationEntryDto;
import com.peace.hospital.entity.outpatientdept.dto.RegistrationFormDto;
import com.peace.hospital.opdept.service.common.IAddressService;
import com.peace.hospital.opdept.service.common.IDepartmentService;
import com.peace.hospital.opdept.service.common.IPeopleService;
import com.peace.hospital.opdept.service.common.IStaffinfoService;
import com.peace.hospital.opdept.service.common.impl.DepartmentServiceImpl;
import com.peace.hospital.opdept.service.common.impl.PeopleServiceImpl;
import com.peace.hospital.opdept.service.outpatientdept.IChargingitemsService;
import com.peace.hospital.opdept.service.outpatientdept.IParticularsService;
import com.peace.hospital.opdept.service.outpatientdept.IPatientinformationService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.List;

/**
 * <p>
 * 挂号信息表 前端控制器
 * </p>
 *
 * @author 谢晓峰
 * @since 2019-11-11
 */
@RestController
@RequestMapping("/patientinformation")
public class PatientinformationController {
/**
 * 病人表*/
    @Autowired
    private IPeopleService ipeopleService;
    /**挂号处方表*/
    @Autowired
private IChargingitemsService iChargingitemsService;
/**挂号信息表*/
@Autowired
private IPatientinformationService iPatientinformationService;
/**
 * 收费类型表*/
@Autowired
private IParticularsService iParticularsService;
/**
 * 科室表*/
@Autowired
private IDepartmentService iDepartmentService;
/**
 * 医生表*/
@Autowired
private IStaffinfoService iStaffinfoService;
/**
 * 地址表*/
@Autowired
private IAddressService iAddressService;
/**
 *添加挂号信息
 * */
@RequestMapping(value = "/add",method = RequestMethod.POST)
public String addPeopleRegister(@RequestBody RegistrationEntryDto registrationEntryDto) {
    System.out.println(registrationEntryDto);
    People people = ipeopleService.selectPeopleByPeople(registrationEntryDto.getPeople());
    if (people == null) {//没有该病人信息
            //添加病人信息
            ipeopleService.addPeople(registrationEntryDto.getPeople());
        //添加peopleId
        People p = ipeopleService.selectPeopleByPeople(registrationEntryDto.getPeople());
       return JSON.toJSONString(addRegistrationFormDto(registrationEntryDto,p));
    }else {
        return JSON.toJSONString(addRegistrationFormDto(registrationEntryDto,people));
    }

}
/**退号操作*/
@RequestMapping(value = "/delete/{patid}",method = RequestMethod.PUT)
public String depLogin(@PathVariable("patid") Integer patid) {
   return iPatientinformationService.updatePatientinformationById(patid)>0?JSON.toJSONString(new ResponseBean(200,"success",iPatientinformationService.updatePatientinformationById(patid))):
            JSON.toJSONString(new ResponseBean(411,"fail"));

}

    /**
     * 通过日期查询挂号信息*/
    @RequestMapping(value = "/select",method = RequestMethod.GET,consumes="application/json")
    public String findPeopleRegisterByDate(@RequestBody Patientinformation patientinformation) {
        List<RegisteredEnquiryDto> registeredEnquiryDtos = iPatientinformationService.selectPeopleRegisterByDate(patientinformation);
        if(registeredEnquiryDtos!=null)
        for (RegisteredEnquiryDto registeredEnquiryDto : registeredEnquiryDtos) {
            registeredEnquiryDto.setParticulars(iParticularsService.findParticularsByParId(registeredEnquiryDto.getPatientinformation().getParchargeitemid()));
        }else
            return null;
        return JSON.toJSONString(new ResponseBean(200,"SUCCESS",registeredEnquiryDtos));
    }

private ResponseBean addRegistrationFormDto(RegistrationEntryDto registrationEntryDto,People p) {
    Chargingitems chargingitems = registrationEntryDto.getChargingitems();
    chargingitems.setPid(p.getPid());
    Patientinformation patientinformation = registrationEntryDto.getPatientinformation();
    patientinformation.setPid(p.getPid());
    if(iPatientinformationService.selectPatientinformationOne(patientinformation)!=null){//如果有挂号信息
        return new ResponseBean(402,"您今日已挂号！");
    }
    //通过科室id查询科室信息
    Integer depid = registrationEntryDto.getDepartment().getDepid();
    Department department = iDepartmentService.findDepartmentById(depid);
    //通过科室id查询余号多的对应医生
    Staffinfo staffinfo = iStaffinfoService.findDoctorBydeptId(depid);
    //添加对应医生id属性
    chargingitems.setStaid(staffinfo.getStaid());
    patientinformation.setStaid(staffinfo.getStaid());
    //通过类型id查询收费类型信息
    Particulars particulars = iParticularsService.findParticularsByParId(chargingitems.getParchargeitemid());
    chargingitems.setChaname(particulars.getPardrugname());
    chargingitems.setChacost(particulars.getParprice());
    //查询科室对应地址
    Address address = iAddressService.selectAddressBydepId(depid);
    patientinformation.setParchargeitemid(chargingitems.getParchargeitemid());
    //插入处方收费表&挂号信息表
    iChargingitemsService.addChargingitems(chargingitems);
    iPatientinformationService.addPatientinformation(patientinformation);

//设置返回的挂号单数据
    RegistrationFormDto dto = new RegistrationFormDto();
    dto.setAddress(address);
    dto.setChargingitems(chargingitems);
    dto.setDepartment(department);
    dto.setParticulars(particulars);
    dto.setPatientinformation(registrationEntryDto.getPatientinformation());
    dto.setPeople(p);
    dto.setStaffinfo(staffinfo);
    return new ResponseBean(200,"success",dto);
}
}
